
import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

//import components
//view page warp component
import viewPageComponent from 'pages/App'

//404
import noPageComponent from 'pages/error/404'
//base channel
import baseTableComponent from 'pages/channel/list'
//save channel
import saveTableComponent from 'pages/channel/save'

Vue.use(VueRouter)

//使用AMD方式加载
// component: resolve => require(['pages/home'], resolve),
const routes = [{
  path: '/404',
  name: 'notPage',
  component: noPageComponent
}, {
  path: '*',
  redirect: '/404'
}, {
  path: '/',
  redirect: '/channel/list',
  component: viewPageComponent,
  children: [{
    path: '/channel/list',
    name: 'tableList',
    component: baseTableComponent,
    meta: {
      title: "频道列表",
    }
  }, {
    path: '/channel/update/:liveGuid',
    name: 'tableUpdate',
    component: saveTableComponent,
    meta: {
      title: "编辑频道",
    }
  }, {
    path: '/channel/add',
    name: 'tableAdd',
    component: saveTableComponent,
    meta: {
      title: "添加频道",
    }
  }]
}]

const router = new VueRouter({
  routes,
  mode: 'hash', //default: hash ,history
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return {x: 0, y: 0}
    }
  }
})

//全局路由配置
//路由开始之前的操作
router.beforeEach((to, from, next) => {
  NProgress.done().start()
  next()
})

//路由完成之后的操作
router.afterEach(route => {
  NProgress.done()
})

export default router
